const AdminBiz = require('../../../../../../comm/biz/admin_biz.js');
const pageHelper = require('../../../../../../helper/page_helper.js');
const cloudHelper = require('../../../../../../helper/cloud_helper.js');
const validate = require('../../../../../../helper/validate.js');
const AdminRoomBiz = require('../../../../biz/admin_room_biz.js');
const formSetHelper = require('../../../../../../cmpts/public/form/form_set_helper.js');
const projectSetting = require('../../../../public/project_setting.js');

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		isLoad: false,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function (options) {
		if (!AdminBiz.isAdmin(this)) return;
		if (!pageHelper.getOptions(this, options)) return;

		wx.setNavigationBarTitle({
			title: projectSetting.ROOM_NAME + '-修改',
		});

		this._loadDetail();
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () { },

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () { },

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () { },

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () { },

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: async function () {
		await this._loadDetail();
		//this.selectComponent("#cmpt-form").reload();
		wx.stopPullDownRefresh();
	},

	model: function (e) {
		pageHelper.model(this, e);
	},

	_loadDetail: async function () {
		if (!AdminBiz.isAdmin(this)) return;

		let id = this.data.id;
		if (!id) return;

		if (!this.data.isLoad) this.setData(AdminRoomBiz.initFormData(id)); // 初始化表单数据

		let params = {
			id
		};
		let opt = {
			title: 'bar'
		};
		let room = await cloudHelper.callCloudData('admin/room_detail', params, opt);
		if (!room) {
			this.setData({
				isLoad: null
			})
			return;
		};

		this.setData({
			isLoad: true,
			formTitle: room.ROOM_TITLE,
			// formCateId: room.ROOM_CATE_ID,
			formOrder: room.ROOM_ORDER,
			formSeatCnt: room.ROOM_SEAT_CNT,
			formAddress: room.ROOM_ADDRESS,
			formAddressGeo: room.ROOM_ADDRESS_GEO,
			formForms: room.ROOM_FORMS
		});
	},

	bindFormSubmit: async function () {
		if (!AdminBiz.isAdmin(this)) return;

		// 数据校验
		let data = this.data;

		let forms = this.selectComponent("#cmpt-form").getForms(true);
		data.formForms = forms;

		for(let k = 0;k < (forms || []).length; k ++) {
			if (forms[k].mark == 'seats') {
				let seatInfo = AdminRoomBiz.getSeatInfo(forms[k].val)
				data.formSeatCnt = seatInfo.seatCnt
				data.formSeatList = seatInfo.seatList
			}
		}

		data = validate.check(data, AdminRoomBiz.CHECK_FORM, this);
		if (!data) return;

		try {
			let roomId = this.data.id;
			data.id = roomId;

			// 先修改，再上传 
			await cloudHelper.callCloudSumbit('admin/room_edit', data).then(res => {
				// 更新列表页面数据
				let node = {
					'ROOM_TITLE': data.title,
					// 'ROOM_CATE_NAME': data.cateName,
					'ROOM_ORDER': data.order,
					'ROOM_SEAT_CNT': data.seatCnt,
					'ROOM_SEAT_LIST': data.seatList,
					'ROOM_ADDRESS': data.address,
					'ROOM_ADDRESS_GEO': data.addressGeo,
				}
				pageHelper.modifyPrevPageListNodeObject(roomId, node);
			});

			await cloudHelper.transFormsTempPics(forms, 'room/', roomId, 'admin/room_update_forms');

			let callback = () => {
				wx.navigateBack();
			}
			pageHelper.showSuccToast('修改成功', 2000, callback);
		} catch (err) {
			console.log(err);
		}
	},

	bindMapTap: function (e) {
		AdminRoomBiz.selectLocation(this);
	},

	url: function (e) {
		pageHelper.url(e, this);
	},

	switchModel: function (e) {
		pageHelper.switchModel(this, e);
	},
})